package com.demo.web; /**
 * @author wzm
 * @create 2023-05-04 14:28
 */

import com.demo.pojo.User;
import com.demo.service.UserService;
import com.demo.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //2.获取请求的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //3.封装为对象
        User user = new User(username,password);

        //4.调用Service方法
       boolean flag = userService.register(user);

        //5.得到结果 判断返回
        if(flag){
            //成功
            request.getRequestDispatcher("/login.html").forward(request,response);
        }else{
            //失败
            request.getRequestDispatcher("/register.html").forward(request,response);
        }

    }
}
